package com.example.italkapp2.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.italkapp2.R;
import com.example.italkapp2.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

//创建新群
public class NewGroupActivity extends Activity{
    private EditText et_newgroup_name;
    private EditText et_newgroup_desc;
    private CheckBox cb_newgroup_public;
    private CheckBox cb_newgroup_invite;
    private Button btn_newgroup_create;
    private EaseTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_group);

        initView();
        initListener();
    }

    private void initListener() {
        btn_newgroup_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewGroupActivity.this, PickContactActivity.class);
                startActivityForResult(intent,1);
            }
        });

        titleBar.setLeftLayoutClickListener(v -> NewGroupActivity.this.finish());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //成功获取到联系人
        if(resultCode==RESULT_OK){
            //创建群
            createGroup(data.getStringArrayExtra("members"));
        }
    }

    private void createGroup(String[] members) {
        String groupName= et_newgroup_name.getText().toString();
        String groupDesc= et_newgroup_desc.getText().toString();

        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //去环信服务器创建群

                EMGroupOptions options=new EMGroupOptions();

                options.maxUsers=200;//群最多容纳200人

                EMGroupManager.EMGroupStyle groupStyle=null;
                if(cb_newgroup_public.isChecked()){//是否公开
                    if(cb_newgroup_invite.isChecked()){//开放群邀请
                        groupStyle=EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                    }else {
                        groupStyle=EMGroupManager.EMGroupStyle.EMGroupStylePublicJoinNeedApproval;
                    }
                }else {
                    if(cb_newgroup_invite.isChecked()){
                        groupStyle=EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                    }else {
                        groupStyle=EMGroupManager.EMGroupStyle.EMGroupStylePrivateOnlyOwnerInvite;
                    }
                }
                options.style=groupStyle;
                try {
                    //1、群名称 2、群描述 3、群成员 4、原因 5、参数设置
                    EMClient.getInstance().groupManager().createGroup(groupName,groupDesc,members,"申请加入群",options);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(NewGroupActivity.this,"创建群成功！",Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(NewGroupActivity.this,"创建群失败！"+e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }


    private void initView() {
        et_newgroup_name=findViewById(R.id.et_newgroup_name);
        et_newgroup_desc=findViewById(R.id.et_newgroup_desc);
        cb_newgroup_public=findViewById(R.id.cb_newgroup_public);
        cb_newgroup_invite=findViewById(R.id.cb_newgroup_invite);
        btn_newgroup_create=findViewById(R.id.btn_newgroup_create);
        titleBar=findViewById(R.id.tb_invite_title);
    }
}